/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server;

import java.sql.Time;
import models.*;
import models.Card.*;
import models.Skill.*;
import models.Skill.Instant.general.SkillInstantSha;

/**
 *处理出牌的线程
 * @author Invince
 */
public class 舍弃版本_Cast_Card implements Runnable{

    public synchronized void run() {//？？？？？？？？？？记得synchornized验证一下
        Server_Public_Data.MessageCast mCast=Server_Public_Data.castQueue.remove();//注意，取不到会报错的，所以顺序一定要注意，先放到messageBox，在唤醒Deck_Process
        Player sourcePlayer=Server_Public_Data.game.getListPlayer().get(mCast.getSourcePlayerID());
        Player targetPlayer=Server_Public_Data.game.getListPlayer().get(mCast.getTargetPlayerID());
        Card castCard=sourcePlayer.getDeckPlayer().get(mCast.getCardID());
        Skill castSkill=castCard.getListSkill().get(mCast.getSpellID());

        Server_Public_Data.refreshScreen();//刷新屏幕，显示谁出了什么牌。。。

        boolean doIt=true;

        /*?????????????验证下面代码的逻辑*/
        Long tBegin=System.currentTimeMillis();
        Long tEnd=System.currentTimeMillis();
        while((tEnd-tBegin)<15*1000){//15*1000 ms
            Server_Public_Data.MessageCast mResponse=Server_Public_Data.responseQueue.poll();//poll returns null if not obtain
            if(mResponse!=null){//means there si a response
                Player tempPlayer=Server_Public_Data.game.getListPlayer().get(mResponse.getSourcePlayerID());
                Card responseCard=tempPlayer.getDeckPlayer().get(mResponse.getCardID());
                Skill responseSkill=responseCard.getListSkill().get(mResponse.getSpellID());
                doIt&=handleResult(castCard, castSkill, responseCard, responseSkill);
                tBegin=System.currentTimeMillis();
                Server_Public_Data.refreshScreen();//刷新屏幕，显示谁出了什么牌。。。
            }
            tEnd=System.currentTimeMillis();
        }
    }

    /**
     * 处理回应牌
     * @param castCard
     * @param castSkill
     * @param responseCard
     * @param responseSkill
     * @return
     */
    public boolean handleResult(Card castCard,Skill castSkill,Card responseCard,Skill responseSkill){
        if(castCard instanceof CardSpell){
            if(responseCard.getName().equals("无懈可击")){
               //把出过的牌扔到弃牌堆是否在这里？？？？？？？？？？
                return false;
            }
        }
        if((castCard.getName().equals("杀")) && (castSkill instanceof SkillInstantSha)){
            if(responseCard.getName().equals("闪")){
                 //把出过的牌扔到弃牌堆是否在这里？？？？？？？？？？
                return false;
            }
        }
        return true;
    }


}
